import os
import time
import json
from datetime import datetime

import streamlit as st

from tools.book import Book
from tools.config_manager import ConfigManager
from tools.crawler import WebCrawler
from tools.downloader import Downloader
from tools.library_manager import LibraryManager, DownloadScanner
from tools.metadata_fetcher import MetadataFetcher

config = ConfigManager()
crawler = WebCrawler()
downloader = Downloader()
library_manager = LibraryManager()
metadata_fetcher = MetadataFetcher()
download_scanner = DownloadScanner()


def home_page():
    st.title("🎉Welcome !")

    # 加入一些emoji
    st.subheader("简介")
    st.write(
        "欢迎使用轻小说文库epub下载器，本工具基于[轻小说文库](https://www.wenku8.net/)和[Streamlit](https://streamlit.io/)构建。可以用于下载轻小说文库的小说并保存为epub格式。")
    st.image(os.path.join(os.getcwd(), 'docs', 'pics', 'search_page.png'))
    st.page_link(st.Page(search_by_id_page), label="开始使用", icon=":material/arrow_forward:",
                 use_container_width=True)
    st.write("**功能列表**")
    st.write("- ✅ 查询文库中的小说信息（支持按编号、书名、作者查询）")
    st.write("- ✅ 下载整本小说或分卷下载")
    st.write("- ✅ 单独下载小说插图")
    st.write("- ✅ 支持配置下载选项")

    st.write("")

    st.write("**注意事项**")
    st.write("- ❗ 本工具仅用于学习交流，请勿滥用，遵守相关法律法规")
    st.write("- ⚠️ 暂不支持下载已下架小说")

    st.write("")


    st.write("**关于**")
    st.write(
        "如果有任何问题或建议，欢迎在GitHub上提出。如果觉得好用，欢迎给个Star⭐。")
    st.page_link("https://github.com/mj3622/Wenku8Downloader", label="GitHub 项目地址", icon=":material/link:",
                 use_container_width=True)


def config_page():
    """
    配置页面
    :return:
    """
    st.title("配置")
    username = st.text_input('请输入轻小说文库的用户名', config.get('login', 'username'),
                             help="当共享账号出现问题时，可以切换为自己的账号")
    password = st.text_input('请输入轻小说文库的密码', config.get('login', 'password'), type='password')

    download_choice = config.get('download', 'full_title')

    index_map = {'FULL': 0, 'IN': 1, 'OUT': 2}

    download_name = st.selectbox('下载小说书名的格式', ['FULL', 'IN', 'OUT'], index=index_map[download_choice])

    wait_time = st.slider("请求等待时间", 0, 10, config.get('download', 'wait_time'))

    with st.container(border=True):
        st.write('以 `败北女角太多了！(败犬女主太多了！)` 为例')
        st.write('FULL: `败北女角太多了！(败犬女主太多了！)`')
        st.write('IN: `败犬女主太多了！`')
        st.write('OUT: `败北女角太多了！`')

        cover_index = st.text_input('封面图片的索引', config.get('download', 'default_cover_index'),
                                    help="默认为0，即第一张插图", type='default')

    t1, c1, c2, t2 = st.columns([1, 4, 2, 1], vertical_alignment='center')

    container = st.container()

    with c1:
        if st.button("保存信息"):
            if not str.isdigit(cover_index):
                st.error("封面图片的索引必须为整数")
                return

            config.set('login', 'username', username)
            config.set('login', 'password', password)
            config.set('download', 'full_title', download_name)
            config.set('download', 'default_cover_index', int(cover_index))
            config.set('download', 'wait_time', wait_time)
            with container:
                st.success("保存成功")
    with c2:
        if st.button("更新cookie"):
            with container:
                with st.spinner('正在更新cookie...'):
                    crawler.get_cookie()
                    st.success("Cookie更新成功")


def full_volumes_download_page():
    """
    整本下载页面
    :return:
    """
    st.title("整本下载")
    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')

    info_container = st.container()
    button_container = st.container()
    status_bar = st.container()

    book = st.session_state.book

    with c1:
        id = st.text_input('请输入轻小说文库的作品编号或链接', help="例如：3057 或 https://www.wenku8.net/book/3057.htm")

        if id.startswith('https://www.wenku8.net/book/'):
            id = id.split('/')[-1].split('.')[0]

    with c2:
        if st.button("查询信息"):
            with status_bar:
                with st.spinner('正在查询中...'):
                    if safe_get_book_id() == id:
                        book = st.session_state.book
                    else:
                        book = Book(id)
                        update_session(book)
    if book is not None:
        with info_container:
            with st.spinner('Wait for it...'):
                show_container = st.container()
                show_book_info(book, show_container)

        with button_container:
            if st.button("打包下载全卷", use_container_width=True):
                downloader.download_novel(book, status_bar)
                st.success("下载完成")
            if st.button("下载全部分卷", use_container_width=True):
                downloader.download_novel(book, status_bar, volume_name="分别下载分卷")
                st.success("下载完成")


def divided_volumes_download_page():
    """
    分卷下载页面
    :return:
    """
    st.title("分卷下载")
    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')

    select_box = st.container()
    status_bar = st.container()

    book = st.session_state.book

    with c1:
        id = st.text_input('请输入轻小说文库的作品编号或链接', help="例如：3057 或 https://www.wenku8.net/book/3057.htm")

        if id.startswith('https://www.wenku8.net/book/'):
            id = id.split('/')[-1].split('.')[0]

    with c2:
        if st.button("查看信息"):
            with status_bar:
                with st.spinner('正在查询中...'):
                    if safe_get_book_id() == id:
                        book = st.session_state.book
                    else:
                        book = Book(id)
                        update_session(book)

    if book is not None:
        with select_box:
            c_s1, c_s2 = st.columns([1, 3])
            with c_s1:
                st.image(book.basic_info['cover'])
            with c_s2:
                st.subheader(book.basic_info['标题'])

                all_volumes = []
                # all_volumes = ["全卷下载"]
                all_volumes += book.volumes.keys()

                selected = st.selectbox('选择要下载的卷', all_volumes)

                st.write('')
                st.write('')
                st.write('')

                if st.button("下载", use_container_width=True, help="默认会以第一张插图作为封面，可在配置页面修改"):
                    downloader.download_novel(book, status_bar, selected)


def picture_download_page():
    """
    图片下载页面
    :return:
    """
    st.title("图片下载")
    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')

    select_box = st.container()
    status_bar = st.container()

    book = st.session_state.book

    with c1:
        id = st.text_input('请输入轻小说文库的作品编号或链接', help="例如：3057 或 https://www.wenku8.net/book/3057.htm")

        if id.startswith('https://www.wenku8.net/book/'):
            id = id.split('/')[-1].split('.')[0]

    with c2:
        if st.button("查看图片"):
            with status_bar:
                with st.spinner('正在查询中...'):
                    if safe_get_book_id() == id:
                        book = st.session_state.book
                    else:
                        book = Book(id)
                        update_session(book)

    if book is not None:
        with select_box:
            c_s1, c_s2 = st.columns([1, 3])
            with c_s1:
                st.image(book.basic_info['cover'])
            with c_s2:
                st.subheader(book.basic_info['标题'])

                selected = st.selectbox('选择要下载的卷', book.volumes.keys())
                st.write('')
                st.write('')
                st.write('')

                t0, c11, c12 = st.columns([1, 2, 2])

                with c11:
                    if st.button("单卷下载"):
                        with status_bar:
                            urls = book.get_chapter_image_urls(volume_name=selected)
                            downloader.download_pictures(urls, selected, book.basic_info['标题'], status_bar)
                            st.success("下载完成")

                with c12:
                    if st.button("全部下载"):
                        with status_bar:
                            start = time.time()
                            for i, (volume, url) in enumerate(book.picture_urls.items(), start=1):
                                print(f"Downloading {volume}...")
                                ch_urls = book.get_chapter_image_urls(volume)
                                t_container = st.container()
                                downloader.download_pictures(ch_urls, volume, book.basic_info['标题'], t_container, i)
                            st.success("下载完成，共耗时：" + time.strftime("%M分%S秒", time.gmtime(time.time() - start)))


def search_by_author_page():
    """
    按作者搜索
    :return:
    """
    st.title("按作者搜索")
    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')

    select_container = st.container()
    info_container = st.container()
    show_books = st.session_state.author_search

    with c1:
        author = st.text_input('请输入轻小说文库的作者', help="例如：橘公司")
        author.strip()

    with c2:
        if st.button("查询信息"):
            with info_container:
                with st.spinner('正在查询中...'):
                    show_books = crawler.search(author, 'author')
                    st.session_state.author_search = show_books
                    if not show_books:
                        st.error("未找到相关作品")

    if len(show_books) > 0:
        author_list = [item['title'] for item in show_books]
        with select_container:
            cs_1, cs_2 = st.columns([3, 1], vertical_alignment='bottom')

            with cs_1:
                selected = st.selectbox('选择要查看的作品', author_list)

            with cs_2:
                if st.button("查看信息"):
                    for item in show_books:
                        if item['title'] == selected:
                            book = Book(item['id'])
                            update_session(book)
                    st.switch_page(st.Page(search_by_id_page))

        for item in show_books:
            with st.container():
                ct1, ct2 = st.columns([1, 3])
                with ct1:
                    st.image(item['cover'])
                with ct2:
                    st.write(f"<h3>{item['title']}</h3>", unsafe_allow_html=True)
                    st.write('---')
                    st.write(f"编号：{item['id']}")
                st.write('---')


def search_by_title_page():
    """
    按书名搜索
    :return:
    """
    st.title("按书名搜索")
    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')

    select_container = st.container()
    info_container = st.container()
    show_books = st.session_state.title_search

    with c1:
        title = st.text_input('请输入轻小说文库的作品名称', help="例如：败犬女主")
        title.strip()

    with c2:
        if st.button("查询信息"):
            with info_container:
                with st.spinner('正在查询中...'):
                    show_books = crawler.search(title, 'articlename')
                    st.session_state.title_search = show_books
                    if not show_books:
                        st.error("未找到相关作品")

    if len(show_books) > 0:
        title_list = [item['title'] for item in show_books]
        with select_container:
            cs_1, cs_2 = st.columns([3, 1], vertical_alignment='bottom')

            with cs_1:
                selected = st.selectbox('选择要查看的作品', title_list)

            with cs_2:
                if st.button("查看信息"):
                    for item in show_books:
                        if item['title'] == selected:
                            book = Book(item['id'])
                            update_session(book)
                    st.switch_page(st.Page(search_by_id_page))

        for item in show_books:
            with st.container():
                ct1, ct2 = st.columns([1, 3])
                with ct1:
                    st.image(item['cover'])
                with ct2:
                    st.write(f"<h3>{item['title']}</h3>", unsafe_allow_html=True)
                    st.write('---')
                    st.write(f"编号：{item['id']}")
                st.write('---')


def search_by_id_page():
    st.title("编号搜索")

    c1, c2 = st.columns([6, 1], vertical_alignment='bottom')
    info_container = st.container()
    jump_container = st.container(border=True)

    with c1:
        id = st.text_input('请输入轻小说文库的作品编号或链接',
                           help="例如：3057 或 https://www.wenku8.net/book/3057.htm")

        if id.startswith('https://www.wenku8.net/book/'):
            id = id.split('/')[-1].split('.')[0]

    with (c2):
        if st.button("查询信息"):
            with info_container:
                with st.spinner('正在查询中...'):
                    if safe_get_book_id() != id:
                        book = Book(id)
                        update_session(book)

    if st.session_state.book is not None:
        with info_container:
            book = st.session_state.book
            result = book.basic_info
            description = result.get('简介')

            with st.spinner('Wait for it...'):
                # 左边显示封面，右边显示信息
                show_container = st.container()
                show_book_info(book, show_container)
                st.write('**简介：**')

                for e in description.split('\n'):
                    st.write(e)

        with jump_container:
            b1, t1, b2, t2, b3, t3, b4 = st.columns([1, 1, 1, 1, 1, 1, 1], vertical_alignment='center')

            with b1:
                st.page_link(st.Page(full_volumes_download_page), label="整本下载",
                             icon=":material/collections_bookmark:")
            with b2:
                st.page_link(st.Page(divided_volumes_download_page), label="分卷下载",
                             icon=":material/library_books:")
            with b3:
                st.page_link(st.Page(picture_download_page), label="图片下载", icon=":material/image_search:")
            with b4:
                if st.button("📥 添加到队列", use_container_width=True):
                    downloader = Downloader()
                    try:
                        task_id = downloader.add_to_queue(book.book_id, book.basic_info['标题'], 'full')
                        st.success(f"已添加到队列！任务ID: {task_id}")
                    except Exception as e:
                        st.error(f"添加到队列失败: {str(e)}")


def debug_page():
    st.title("调试")
    st.write("**本页面用于调试，如有需要可以在app.py中更新debug_page函数**")


def update_session(book):
    for key in st.session_state.keys():
        del st.session_state[key]

    st.session_state.book = book
    st.session_state.multiselect = list(book.volumes.keys())


def safe_get_book_id():
    if st.session_state.book is not None:
        return getattr(st.session_state.book, 'book_id', -1)
    return -1


def show_book_info(book, container):
    """
    显示书籍的基本信息
    :param book: 一个Book对象
    :param container: 显示的容器
    :return:
    """
    with container:
        pic_col, info_col = st.columns([3, 5], vertical_alignment='bottom')
        with pic_col:
            st.image(book.basic_info['cover'])
        with info_col:
            st.subheader(book.basic_info['标题'])
            key_col, value_col = st.columns([2, 7])
            for key, value in book.basic_info.items():
                if key == 'cover' or key == '标题' or key == '简介':
                    continue
                key_col.write('**' + key + '：**')
                value_col.write(value)


def encode_key(key):
    """
    将关键字编码为URL编码
    :param key: 待编码的关键字
    :return: 编码后的字符串
    """
    encoded_bytes = key.encode('gbk')
    return ''.join([f'%{byte:02x}' for byte in encoded_bytes])


def library_page():
    """本地图书馆页面"""
    st.title("📚 本地图书馆")
    
    # 顶部操作栏
    col1, col2, col3, col4 = st.columns([2, 1, 1, 1])
    
    with col1:
        search_query = st.text_input("🔍 搜索书籍", placeholder="输入书名、作者或标签...")
    
    with col2:
        if st.button("🔄 扫描下载目录"):
            with st.spinner("正在扫描下载目录..."):
                added_count = download_scanner.add_existing_downloads()
                if added_count > 0:
                    st.success(f"成功添加 {added_count} 本已下载书籍到数据库")
                else:
                    st.info("未发现新的下载书籍")
    
    with col3:
        if st.button("📊 统计信息"):
            stats = library_manager.get_statistics()
            if stats:
                st.success(f"总计 {stats['total_books']} 本书籍 ({stats['total_size_gb']} GB)")
    
    with col4:
        if st.button("🧹 清理数据库"):
            if st.checkbox("确认删除所有已不存在的书籍记录"):
                # 这里可以添加清理逻辑
                st.info("清理功能待实现")
    
    st.markdown("---")
    
    # 搜索过滤器
    filter_col1, filter_col2 = st.columns(2)
    with filter_col1:
        author_filter = st.text_input("作者过滤", placeholder="输入作者名...")
    with filter_col2:
        publisher_filter = st.text_input("出版社过滤", placeholder="输入出版社...")
    
    # 搜索书籍
    filters = {}
    if author_filter:
        filters['author'] = author_filter
    if publisher_filter:
        filters['publisher'] = publisher_filter
    
    books = library_manager.search_books(search_query, filters)
    
    # 显示统计信息
    if books:
        stats = library_manager.get_statistics()
        col1, col2, col3, col4 = st.columns(4)
        with col1:
            st.metric("书籍总数", stats['total_books'])
        with col2:
            st.metric("总大小", f"{stats['total_size_gb']} GB")
        with col3:
            st.metric("作者数量", len(stats['authors']))
        with col4:
            st.metric("出版社数量", len(stats['publishers']))
    
    # 显示书籍列表
    if books:
        st.subheader(f"找到 {len(books)} 本书籍")
        
        for book in books:
            with st.container(border=True):
                col1, col2, col3 = st.columns([1, 3, 1])
                
                with col1:
                    # 显示封面图片（如果存在）
                    if book.get('cover_url'):
                        st.image(book['cover_url'], width=100)
                    else:
                        st.image("https://via.placeholder.com/100x150?text=暂无封面", width=100)
                
                with col2:
                    st.subheader(book['title'])
                    
                    # 书籍信息
                    info_col1, info_col2 = st.columns(2)
                    with info_col1:
                        if book.get('author'):
                            st.write(f"**作者:** {book['author']}")
                        if book.get('publisher'):
                            st.write(f"**出版社:** {book['publisher']}")
                    
                    with info_col2:
                        if book.get('file_size'):
                            size_mb = round(book['file_size'] / (1024*1024), 2)
                            st.write(f"**大小:** {size_mb} MB")
                        if book.get('download_date'):
                            st.write(f"**下载时间:** {book['download_date'][:10]}")
                    
                    # 标签显示
                    if book.get('tags'):
                        tag_text = ", ".join(book['tags'])
                        st.write(f"**标签:** {tag_text}")
                    
                    # 简介（截断显示）
                    if book.get('description'):
                        desc = book['description']
                        if len(desc) > 100:
                            desc = desc[:100] + "..."
                        st.write(f"**简介:** {desc}")
                
                with col3:
                    # 操作按钮
                    if st.button("📖 阅读", key=f"read_{book['id']}"):
                        st.info("在线阅读功能待实现")
                    
                    if st.button("🔄 更新信息", key=f"refresh_{book['id']}"):
                        with st.spinner("正在更新元数据..."):
                            new_metadata = metadata_fetcher.refresh_book_metadata(
                                book['title'], book.get('author')
                            )
                            if new_metadata:
                                if library_manager.update_book_metadata(book['book_id'], new_metadata):
                                    st.success("元数据更新成功")
                                    st.rerun()
                                else:
                                    st.error("更新失败")
                            else:
                                st.warning("未找到匹配的书籍信息")
                    
                    if st.button("🗑️ 删除", key=f"delete_{book['id']}"):
                        if st.checkbox("确认删除这条记录？", key=f"confirm_{book['id']}"):
                            if library_manager.delete_book(book['book_id']):
                                st.success("记录已删除")
                                st.rerun()
                            else:
                                st.error("删除失败")
                    
                    # 文件操作
                    if os.path.exists(book['download_path']):
                        with open(book['download_path'], "rb") as f:
                            st.download_button(
                                label="📥 下载文件",
                                data=f,
                                file_name=os.path.basename(book['download_path']),
                                mime="application/epub+zip",
                                key=f"download_{book['id']}"
                            )
                    else:
                        st.error("文件不存在")
                
                st.markdown("---")
    else:
        if search_query or filters:
            st.info("未找到匹配的书籍")
        else:
            st.info("图书馆为空，请先扫描下载目录或下载一些书籍")
    
    # 显示统计信息
    if books:
        stats = library_manager.get_statistics()
        col1, col2, col3, col4 = st.columns(4)
        with col1:
            st.metric("书籍总数", stats['total_books'])
        with col2:
            st.metric("总大小", f"{stats['total_size_gb']} GB")
        with col3:
            st.metric("作者数量", len(stats['authors']))
        with col4:
            st.metric("出版社数量", len(stats['publishers']))
    
    # 显示书籍列表
    if books:
        st.subheader(f"找到 {len(books)} 本书籍")
        
        for book in books:
            with st.container(border=True):
                col1, col2, col3 = st.columns([1, 3, 1])
                
                with col1:
                    # 显示封面图片（如果存在）
                    if book.get('cover_url'):
                        st.image(book['cover_url'], width=100)
                    else:
                        st.image("https://via.placeholder.com/100x150?text=暂无封面", width=100)
                
                with col2:
                    st.subheader(book['title'])
                    
                    # 书籍信息
                    info_col1, info_col2 = st.columns(2)
                    with info_col1:
                        if book.get('author'):
                            st.write(f"**作者:** {book['author']}")
                        if book.get('publisher'):
                            st.write(f"**出版社:** {book['publisher']}")
                    
                    with info_col2:
                        if book.get('file_size'):
                            size_mb = round(book['file_size'] / (1024*1024), 2)
                            st.write(f"**大小:** {size_mb} MB")
                        if book.get('download_date'):
                            st.write(f"**下载时间:** {book['download_date'][:10]}")
                    
                    # 标签显示
                    if book.get('tags'):
                        tag_text = ", ".join(book['tags'])
                        st.write(f"**标签:** {tag_text}")
                    
                    # 简介（截断显示）
                    if book.get('description'):
                        desc = book['description']
                        if len(desc) > 100:
                            desc = desc[:100] + "..."
                        st.write(f"**简介:** {desc}")
                
                with col3:
                    # 操作按钮
                    if st.button("📖 阅读", key=f"read_{book['book_id']}"):
                        st.info("在线阅读功能待实现")
                    
                    if st.button("🔄 更新信息", key=f"refresh_{book['book_id']}"):
                        with st.spinner("正在更新元数据..."):
                            new_metadata = metadata_fetcher.refresh_book_metadata(
                                book['title'], book.get('author')
                            )
                            if new_metadata:
                                if library_manager.update_book_metadata(book['book_id'], new_metadata):
                                    st.success("元数据更新成功")
                                    st.rerun()
                                else:
                                    st.error("更新失败")
                            else:
                                st.warning("未找到匹配的书籍信息")
                    
                    if st.button("🗑️ 删除", key=f"delete_{book['book_id']}"):
                        if st.checkbox("确认删除这条记录？", key=f"confirm_{book['book_id']}"):
                            if library_manager.delete_book(book['book_id']):
                                st.success("记录已删除")
                                st.rerun()
                            else:
                                st.error("删除失败")
                    
                    # 文件操作
                    if os.path.exists(book['download_path']):
                        with open(book['download_path'], "rb") as f:
                            st.download_button(
                                label="📥 下载文件",
                                data=f,
                                file_name=os.path.basename(book['download_path']),
                                mime="application/epub+zip",
                                key=f"download_{book['book_id']}"
                            )
                    else:
                        st.error("文件不存在")
                
                st.markdown("---")
    else:
        if search_query or filters:
            st.info("未找到匹配的书籍")
        else:
            st.info("图书馆为空，请先扫描下载目录或下载一些书籍")


def batch_download_page():
    """批量下载页面"""
    st.title("批量下载")
    
    downloader = Downloader()
    
    # Add book to queue section
    st.header("添加书籍到队列")
    
    col1, col2 = st.columns([2, 1])
    with col1:
        book_id = st.text_input('请输入轻小说文库的作品编号或链接', 
                               help="例如：3057 或 https://www.wenku8.net/book/3057.htm")
        
        if book_id.startswith('https://www.wenku8.net/book/'):
            book_id = book_id.split('/')[-1].split('.')[0]
    
    with col2:
        download_type = st.selectbox('下载类型', ['full', 'volume', 'separate_volumes'], 
                                   help="full: 整本下载, volume: 单卷下载, separate_volumes: 分别下载分卷")
    
    volume_name = None
    if download_type == 'volume':
        if st.session_state.book and st.session_state.book.book_id == book_id:
            book = st.session_state.book
            volume_name = st.selectbox('选择卷', list(book.volumes.keys()))
        else:
            st.info("请先查询书籍信息以选择卷")
            if st.button("查询书籍信息"):
                with st.spinner('正在查询中...'):
                    book = Book(book_id)
                    update_session(book)
                    st.rerun()
    
    if st.button("添加到队列"):
        if not book_id or not book_id.strip():
            st.error("请输入有效的书籍编号")
        else:
            try:
                # Get book info for title
                book = Book(book_id)
                task_id = downloader.add_to_queue(book_id, book.basic_info['标题'], download_type, volume_name)
                st.success(f"成功添加到队列！任务ID: {task_id}")
            except Exception as e:
                st.error(f"添加失败: {str(e)}")
    
    st.markdown("---")
    
    # Queue status section
    st.header("下载队列状态")
    
    queue_status = downloader.get_queue_status()
    
    # Queue controls
    col1, col2, col3, col4 = st.columns(4)
    with col1:
        if st.button("▶️ 开始队列" if not queue_status['is_running'] else "⏸️ 暂停队列"):
            if queue_status['is_running']:
                downloader.pause_queue()
            else:
                downloader.resume_queue()
            st.rerun()
    
    with col2:
        if st.button("🔄 刷新状态"):
            st.rerun()
    
    with col3:
        if st.button("🗑️ 清空已完成"):
            downloader.clear_completed()
            st.rerun()
    
    with col4:
        if st.button("📊 导出日志"):
            # Simple log export
            log_data = {
                'queue_status': queue_status,
                'timestamp': datetime.now().isoformat()
            }
            st.download_button(
                label="下载日志",
                data=json.dumps(log_data, indent=2, ensure_ascii=False),
                file_name=f"download_queue_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json",
                mime="application/json"
            )
    
    # Current task progress
    if queue_status['current_task']:
        current_task = queue_status['current_task']
        st.subheader("当前下载任务")
        
        col1, col2 = st.columns([1, 3])
        with col1:
            st.metric("进度", f"{current_task['progress']:.1%}")
        with col2:
            st.progress(current_task['progress'])
        
        st.write(f"**书籍:** {current_task['book_title']} (ID: {current_task['book_id']})")
        st.write(f"**类型:** {current_task['download_type']}")
        if current_task['volume_name']:
            st.write(f"**卷:** {current_task['volume_name']}")
    
    # Queue statistics
    col1, col2, col3, col4 = st.columns(4)
    with col1:
        st.metric("待处理", len(queue_status['pending_tasks']))
    with col2:
        st.metric("处理中", len(queue_status['processing_tasks']))
    with col3:
        st.metric("已完成", len(queue_status['completed_tasks']))
    with col4:
        st.metric("失败", len(queue_status['failed_tasks']))
    
    # Pending tasks
    if queue_status['pending_tasks']:
        st.subheader("待处理任务")
        for task in queue_status['pending_tasks']:
            with st.expander(f"{task['book_title']} - {task['download_type']}"):
                st.write(f"**任务ID:** {task['task_id']}")
                st.write(f"**书籍ID:** {task['book_id']}")
                st.write(f"**下载类型:** {task['download_type']}")
                if task['volume_name']:
                    st.write(f"**卷:** {task['volume_name']}")
                st.write(f"**创建时间:** {task['created_at']}")
                
                if st.button(f"❌ 删除", key=f"del_{task['task_id']}"):
                    if downloader.remove_from_queue(task['task_id']):
                        st.success("任务已删除")
                        st.rerun()
    
    # Completed tasks
    if queue_status['completed_tasks']:
        st.subheader("已完成任务")
        for task in queue_status['completed_tasks']:
            with st.expander(f"✅ {task['book_title']} - {task['download_type']}"):
                st.write(f"**完成时间:** {task['completed_at']}")
                if task['file_paths']:
                    st.write("**文件路径:**")
                    for path in task['file_paths']:
                        st.code(path)
    
    # Failed tasks
    if queue_status['failed_tasks']:
        st.subheader("失败任务")
        for task in queue_status['failed_tasks']:
            with st.expander(f"❌ {task['book_title']} - {task['download_type']}"):
                st.error(f"错误: {task['error_message']}")
                if st.button(f"🔄 重试", key=f"retry_{task['task_id']}"):
                    if downloader.retry_failed_task(task['task_id']):
                        st.success("任务已加入重试队列")
                        st.rerun()


def init():
    """
    初始化函数
    :return:
    """
    if 'book' not in st.session_state:
        st.session_state.book = None
    if 'multiselect' not in st.session_state:
        st.session_state.multiselect = None

    if 'title_search' not in st.session_state:
        st.session_state.title_search = []

    if 'author_search' not in st.session_state:
        st.session_state.author_search = []


if __name__ == "__main__":
    pages = {
        '基本': [
            st.Page(home_page, title='主页', icon=":material/home:"),
            st.Page(config_page, title='配置', icon=":material/settings:")
        ],
        '查询': [
            st.Page(search_by_id_page, title='编号检索', icon=":material/123:"),
            st.Page(search_by_author_page, title='作者检索', icon=":material/person:"),
            st.Page(search_by_title_page, title='书名检索', icon=":material/book:")
        ],
        '下载': [
            st.Page(full_volumes_download_page, title='整本下载', icon=":material/collections_bookmark:"),
            st.Page(divided_volumes_download_page, title='分卷下载', icon=":material/library_books:"),
            st.Page(picture_download_page, title='图片下载', icon=":material/image_search:"),
            st.Page(batch_download_page, title='批量下载', icon=":material/playlist_add:")
        ],
        # '图书馆': [
        #     st.Page(library_page, title='本地图书馆', icon=":material/local_library:")
        # ],
        # 调试页面
        # '调试': [
        #     st.Page(debug_page, title='调试', icon=":material/bug_report:")
        # ]
    }

    init()

    pg = st.navigation(pages)
    pg.run()
